local zhesi = fk.CreateSkill {
  name = "inker_zhesi",
}

local DIY = require "packages.diy_utility.diy_utility"
local IU = require "packages.inker.pkg.util.inker_util"

zhesi:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

Fk:addTargetTip{
  name = zhesi.name,
  target_tip = function(self, player, to_select, selected, selected_cards, card, selectable, extra_data)
    if not selectable then return end
    local banner = Fk:currentRoom():getBanner("_inker_zhesi_cards") or {}
    local actural_num = banner[to_select]
    if actural_num then
      return "#inker_zhesi_num:::" .. actural_num
    end
  end
}

zhesi:addEffect(fk.EventPhaseProceeding, {
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(zhesi.name) and player.phase == Player.Finish) then return false end
    local cids = DIY.getCenterCards(player.room)
    local tmp_cids = {}
    player.room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(cids, info.cardId) then
              if move.moveReason == fk.ReasonUse then
                table.insert(tmp_cids, info.cardId)
              end
            end
          end
        end
      end
    end, nil, Player.HistoryTurn)
    return #tmp_cids * 2 < #cids
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local family_member = table.filter(room:getAlivePlayers(false), function(p)
      return IU.FamilyMember(player, p) and room:getBanner("_inker_zhesi_cards")[p] ~= nil
    end)
    local tos = room:askToChoosePlayers(player, {
      targets = family_member,
      min_num = 1,
      max_num = 1,
      skill_name = zhesi.name,
      prompt = "#inker_zhesi-invoke",
      target_tip_name = zhesi.name,
    })

    if #tos > 0 then
      event:setCostData(self, { tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local to = dat.tos[1]
    if not to or not to:isAlive() then return end
    local handnum = to:getHandcardNum()
    local adjustnum = (room:getBanner("_inker_zhesi_cards") or {})[to] - handnum
    if adjustnum > 0 then
      adjustnum = math.min(adjustnum, 5 - handnum)
      if adjustnum > 0 then room:drawCards(to, adjustnum, zhesi.name) end
    elseif adjustnum < 0 then
      room:askToDiscard(to, {
        min_num = -adjustnum,
        max_num = -adjustnum,
        skill_name = zhesi.name,
      })
    end
  end
})

zhesi:addEffect(fk.TurnStart, {
  is_delay_effect = true,
  global = true,
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = {}
    for _, to in ipairs(room:getAlivePlayers(false)) do
      mark[to] = to:getHandcardNum()
    end
    room:setBanner("_inker_zhesi_cards", mark)
  end,
})

Fk:loadTranslationTable{
  ["inker_zhesi"] = "哲思",
  [":inker_zhesi"] = "宗族技，结束阶段，若中央区以非使用而进入居多，你可以将一名同族角色手牌调整至回合开始时（至多摸至五张）。",

  ["#inker_zhesi-invoke"] = "哲思：你可以将一名同族角色手牌调整至回合开始时。",

  ["#inker_zhesi_num"] = "调整至%arg张",

  ["$inker_zhesi1"] = "兵者，行霸道之势，彰王道之实。",
  ["$inker_zhesi2"] = "将为军魂，可因势而袭，其有战无类。",
}

return zhesi